import datetime
from app import db

# 用户表
class User(db.Model):
    __table_args__ = {'extend_existing': True}
    id = db.Column(db.Integer, primary_key=True)
    phone = db.Column(db.String(20), unique=True, nullable=False)
    openid = db.Column(db.String(100), unique=True, nullable=True)
    nickname = db.Column(db.String(50), nullable=True)
    avatar = db.Column(db.String(200), nullable=True)
    password = db.Column(db.String(255), nullable=True)  # 用于管理员登录
    is_admin = db.Column(db.Boolean, default=False)  # 管理员标记
    admin_level = db.Column(db.Integer, default=0)  # 管理员级别：0-非管理员，1-普通管理员，2-超级管理员
    created_at = db.Column(db.DateTime, default=datetime.now)
    updated_at = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now)
    
    # 关系：一个用户可以有多条评估记录
    assessments = db.relationship('Assessment', backref='user', lazy=True)

# 科普文章表
class Article(db.Model):
    __table_args__ = {'extend_existing': True}
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(100), nullable=False)
    summary = db.Column(db.String(500), nullable=False)
    content = db.Column(db.Text, nullable=False)
    cover_image = db.Column(db.String(200), nullable=True)
    publish_date = db.Column(db.String(20), nullable=False)
    created_at = db.Column(db.DateTime, default=datetime.now)
    updated_at = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now)

# 轮播图表
class Banner(db.Model):
    __table_args__ = {'extend_existing': True}
    id = db.Column(db.Integer, primary_key=True)
    image_url = db.Column(db.String(200), nullable=False)
    link = db.Column(db.String(200), nullable=False)
    order = db.Column(db.Integer, default=0)
    created_at = db.Column(db.DateTime, default=datetime.now)
    updated_at = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now)

# 评估表（包含问卷评估和拍照评估）
class Assessment(db.Model):
    __table_args__ = {'extend_existing': True}
    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=True)  # 允许匿名评估
    assessment_type = db.Column(db.String(20), nullable=False)  # 'questionnaire' 或 'photo'
    
    # 问卷评估相关字段
    eyelid_sign = db.Column(db.String(100), nullable=True)
    right_eye_protrusion = db.Column(db.Float, nullable=True)
    left_eye_protrusion = db.Column(db.Float, nullable=True)
    eye_movement = db.Column(db.String(100), nullable=True)
    
    # 拍照评估相关字段
    photo_path = db.Column(db.String(200), nullable=True)
    
    # 评估结果相关字段
    cas_score = db.Column(db.Float, nullable=True)
    treatment_advice = db.Column(db.Text, nullable=True)
    
    # 状态字段
    status = db.Column(db.String(20), default='pending')  # 'pending', 'processing', 'completed'
    created_at = db.Column(db.DateTime, default=datetime.now)
    updated_at = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now)